#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__author__ = "Administrator"
__name__="consumer_producter"
__date__="2016/7/27"
__time__="14:43"
"""
import threading
import time
import Queue
import random

'''
def Proudcer(name,que):
    while True:
        que.put('baozi')
        print '%s: Made a baozi ...======' %name
        time.sleep(random.randrange(6))

def Consumer(name,que):
    while True:
        try:
            que.get_nowait()    # 取消阻塞，当queue为空时会报错(线程会消失)，故加上异常处理（线程不会消失）
            print '%s: Got a baozi ...' %name
        except Exception:
            print u'没有包子了'
        time.sleep(random.randrange(3))

# 声明一个队列
q = Queue.Queue()

# 启动两个生产者线程
p1 = threading.Thread(target=Proudcer,args=['chushi1',q])
p2 = threading.Thread(target=Proudcer,args=['chushi2',q])
p1.start()
p2.start()

# 启动两个消费者线程
c1 = threading.Thread(target=Consumer,args=['tom',q])
c2 = threading.Thread(target=Consumer,args=['jon',q])
c1.start()
c2.start()
'''
def Proudcer(name,que):
    while True:
        if que.qsize() <3:
            que.put('baozi')
            print '%s: Made a baozi ...======' %name
        else:
            print u'还有3个包子。。。'
        time.sleep(random.randrange(6)) # 可以调整时间查看效果

def Consumer(name,que):
    while True:
        try:
            que.get_nowait()    # 取消阻塞，当queue为空时会报错(线程会消失)，故加上异常处理（线程不会消失）
            print '%s: Got a baozi ...' %name
        except Exception:
            print u'没有包子了'
        time.sleep(random.randrange(3))

# 声明一个队列
q = Queue.Queue()

# 启动两个生产者线程
p1 = threading.Thread(target=Proudcer,args=['chushi1',q])
p2 = threading.Thread(target=Proudcer,args=['chushi2',q])
p1.start()
p2.start()

# 启动两个消费者线程
c1 = threading.Thread(target=Consumer,args=['tom',q])
c2 = threading.Thread(target=Consumer,args=['jon',q])
c1.start()
c2.start()

